package com.qst.unitetest;

import com.qst.bean.wjrBean;
import com.qst.dao.wjrBeanMapper;
import com.qst.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class testIuserMapper {
    @Test
    public void test() {
        String resource = "mybatis-config.xml";
        try {
            InputStream inputStream = Resources.getResourceAsStream(resource);
            // 创建了mybatis的sqlSessionFactory工厂类:读取mybatis.xml配置文件，使用该配置文件中的参数信息，用于创建SqlSession对象
            SqlSessionFactory sqlSessionFactory =
                    new SqlSessionFactoryBuilder().build(inputStream);
            SqlSession session = sqlSessionFactory.openSession();
            // 从mybatis的sqlsession对象中获取dao层的接口的实例对象
            //wjrBeanMapper wjrBeanMapper = session.getMapper(wjrBeanMapper.class);
            // 调用dao层接口的方法进行数据库操作
            //wjrBean wjrBean = wjrBeanMapper.selectByPrimaryKey(3l);

            //获取接口层的代理对象（UserMapper）
            UserMapper UserMapper = session.getMapper(UserMapper.class);
            wjrBean wjrBean = UserMapper.findwjrBeanByid(3L);
            List<wjrBean> list = UserMapper.findAll();
            for (wjrBean bean : list) {
                System.out.println("mybatis查询结果：");
                System.out.println(bean);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}




